#include<iostream>
#include<string>
#include <errno.h>
#include <fcntl.h> // open
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include<sys/wait.h>


using namespace std;


void fun()
{
    cout<<"lala"<<endl;
    exit(10);
}

int main()
{
    pid_t id = fork();
    if(id<0)
    {
        printf("errno : %d, errstring: %s\n", errno, strerror(errno));
        return errno;
    }
    else if(id==0)
    {
        
		int cnt = 5;
        while(cnt)
        {
            printf("子进程运行中， pid: %d\n", getpid());
            cnt--;
            sleep(1);
        }
    }
	else 
    {
        sleep(10);
        pid_t rid = waitpid(-1,nullptr,0); // == wait
        if(rid>0)
        {
            printf("wait sub process sucess, rid = %d\n",rid);
        }
        while(true)
        {
            printf("我是父进程: pid:%d\n", getpid());
            sleep(1);
        }
    }


    // fun();
    // cout<<"进程正常结束"<<endl;

    //// 打开一个不存在的文件就会报错
    //int fd = open("missing_file.txt", O_RDONLY);
    //if(fd == -1)
    //{
    //    printf("open file fail! error:%d, reason:%s\n",errno,strerror(errno));
    //    return 1;
    //}
    //
    //printf("open file sucess");
    


    //cout<<"hello"<<endl;

    return 0;
}
